<?php
/*
 * Page.Class.php
 * @author Nguyen Khac Tu
 * @since 1.0 - 28-07-2009
 * notice: Must have:
 * 		1. Must have hidden tag in form named p
 * 		2. Css of pager can define use set methods to modify or customize Css in .css file
 * 		3. Example view listReceivedMail
 *		4. Can Use default css file (attach in mail)
 */
class Page {
	private $numberRecordsOfPage;
	private $currentPage;
	private $form;
	/*
		* Constructor
		* @author Nguyen Khac Tu
		* @since 1.0 - 28-07-2009
		* 		 
		*/
	public function Page($numberRecordsOfPage, $form) {
		$this->numberRecordsOfPage = $numberRecordsOfPage;
		$this->form = $form;
		$this->getCurrentPage();
	}
	/**
	 * getCurrentPage
	 * - Get current page number
	 * @author Nguyen Khac Tu
	 * @since 1.0 - 30-07-2009
	 */
	private function getCurrentPage() {
		$this->currentPage = 1;
		if (isset ($_REQUEST['p']) && is_numeric($_REQUEST['p']))
			$this->currentPage = $_REQUEST['p'];
	}
	/**
	 * getLinkAll
	 * - Return Html of page
	 * @param int $perSide: number page relate current page to show
	 * @author Nguyen Thanh Quyen
	 * @since 1.0 - 30-07-2009
	 */
	public function getLinkAll($totalRecords = 0, $perSide, $additional = null) {
		$pagerHtml = "<div $additional>";
		$pagerHtml .= "<ul>";
		// Get number of pages
		$currentPage = $this->currentPage;
		$numberOfPages = $this->getNumberOfPages($totalRecords);
		if (($currentPage < 1) || ($currentPage > $numberOfPages)) {
			$currentPage = 1;
		}
		// Case ex : 1...34567...12(persize=2)
		if (($currentPage > ($perSide +1)) && (($currentPage + $perSide) < $numberOfPages)) {
			$pagerHtml .= $this->getLinkForPageLabel(1, true);
			$pagerHtml .= "<li style=\"border:0\">...</li>";
			for ($i = ($currentPage - $perSide); $i <= ($currentPage + $perSide); $i++) {
				if ($currentPage != $i) {
					$pagerHtml .= $this->getLinkForPageLabel($i, true);
				} else {
					$pagerHtml .= $this->getLinkForPageLabel($i, false);
				}
			}
			$pagerHtml .= "<li style=\"border:0\">...</li>";
			$pagerHtml .= $this->getLinkForPageLabel($numberOfPages, true);
		}
		else
		// Case ex : 123456 ... 10(persize=2)
		if (($currentPage <= ($perSide +1)) && (($currentPage + $perSide) < $numberOfPages)) {
			for ($i = 1; $i <= min( $numberOfPages , $currentPage + $perSide ); $i++) {
				if ($currentPage != $i)
					$pagerHtml .= $this->getLinkForPageLabel($i, true);
				else
					$pagerHtml .= $this->getLinkForPageLabel($i, false);
			}
			$pagerHtml .= "<li style=\"border:0\">...</li>";
			$pagerHtml .= $this->getLinkForPageLabel($numberOfPages, true);
		}
		else
		// Case ex : 1...456789(persize=2)
		if (($currentPage > ($perSide +1)) && (($currentPage + $perSide) >= $numberOfPages)) {
			$pagerHtml .= $this->getLinkForPageLabel(1, true);
			$pagerHtml .= "<li style=\"border:0\">...</li>";
			for ($i = max(($currentPage - $perSide), 1); $i <= $numberOfPages; $i++) {
				if ($currentPage != $i)
					$pagerHtml .= $this->getLinkForPageLabel($i, true);
				else
					$pagerHtml .= $this->getLinkForPageLabel($i, false);
			}
		} 
		// Case ex : 12345(persize=2)
		else {
			for ($i = 1; $i <= $numberOfPages; $i++) {
				if ($currentPage != $i)
					$pagerHtml .= $this->getLinkForPageLabel($i, true);
				else
					$pagerHtml .= $this->getLinkForPageLabel($i, false);
			}
		}
		$pagerHtml .= "</ul>";
		$pagerHtml .= "</div>";
		return $pagerHtml;
	}
	private function getNumberOfPages($totalRecords) {
		if ($totalRecords <= 0) {
			return 1;
		}
		$numberRecordsOfPage = $this->numberRecordsOfPage;
		if (($totalRecords % $numberRecordsOfPage) == 0) {
			return ((int) floor($totalRecords / $numberRecordsOfPage));
		} else {
			return ((int) (floor($totalRecords / $numberRecordsOfPage) + 1));
		}
	}
	private function getLinkForPageLabel($pageText, $flag) {
		$pagerHtml = $this->getHtmlOfPageLabel($pageText, $flag);
		return $pagerHtml;
	}
	public function getLinkForNextPage($totalRecords, $additional = null) {
		$pagerHtml = "<div $additional>";
		$this->getCurrentPage();
		if($this->currentPage > (int) ( floor( $totalRecords / $this->numberRecordsOfPage ))) {
			$pageNumber = 1;
		}else {
			$pageNumber = $this->currentPage + 1;
		}
		$pagerHtml .= "<a href=\"#\" onclick=\"document.getElementById('p').value='$pageNumber';document.$this->form.submit()\"><font color=\"#ac2020\">Next</font></a>";
		$pagerHtml .= "</div>";
		return $pagerHtml;
	}
	public function getLinkForPreviousPage($additional = null) {
		$pagerHtml = "<div $additional>";
		$this->getCurrentPage();
		$pageNumber = $this->currentPage;
		if ($pageNumber > 1) {
			$pageNumber = $pageNumber - 1;
		}
		$pagerHtml .= "<a href=\"#\" onclick=\"document.getElementById('p').value='$pageNumber';document.$this->form.submit()\"><font color=\"#ac2020\">Previous</font></a>";
		$pagerHtml .= "</div>";
		return $pagerHtml;
	}
	public function getFormatTotalLabel($total, $addition = null) {
		$pagerHtml .= "<div $addition>";
		$pagerHtml .= $total;
		$pagerHtml .= "</div>";
		return $pagerHtml;
	}
	/**
	 * getHtmlOfPageLabel
	 * @author Nguyen Khac Tu
	 * @since 1.0 - 30-07-2009
	 */
	private function getHtmlOfPageLabel($pageText, $flag) {
		if ($flag) {
			$html = "<li>";
			$html .= "<a href=\"#\" onclick=\"document.getElementById('p').value='$pageText';document.$this->form.submit()\"> $pageText </a>";
			$html .= "</li>";
		} else {
			$html = "<li><font color=\"red\">&nbsp;&nbsp;" . $pageText . "&nbsp;&nbsp;</font></li>";
		}
		return $html;
	}
}
?>